package com.youlu.campus.admin.task;

import com.youlu.campus.admin.utils.DateTimeUtils;
import com.youlu.campus.service.notify.KuaiDi100PushCompensateService;
import com.youlu.campus.service.notify.KuaiDi100Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class Kuaidi100RollBackTask {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private KuaiDi100PushCompensateService kuaiDi100PushCompensateService;

    @Autowired
    private KuaiDi100Service kuaiDi100Service;

    /**
     * ems运单号推送失败补偿
     * 每小时执行一次
     * 每天晚上20：00~23:00触发
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void doAutoCreate(){
        Boolean flag = redisTemplate.opsForValue().setIfAbsent("kuaidi100:push:rollback", "1", 3L, TimeUnit.MINUTES);
        if(Objects.nonNull(flag)&&!flag){
            return;
        }
        log.info("开始执行推送补偿:{}"+ DateTimeUtils.formatDateTime(new Date()));
        kuaiDi100PushCompensateService.job();
        log.info("开始执行推送补偿:{} end"+ DateTimeUtils.formatDateTime(new Date()));

    }

    /**
     * 轨迹拉取补偿
     * 每天凌晨0点执行一次
     */
    @Scheduled(cron = "0 0 0 */2 * ?")
    public void pullTrackTask(){
        Boolean flag = redisTemplate.opsForValue().setIfAbsent("kuaidi100:pull:rollback", "1", 3L, TimeUnit.MINUTES);
        if(Objects.nonNull(flag)&&!flag){
            return;
        }
        kuaiDi100Service.pullTrackTask();
    }
}
